package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.utils.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    @Qualifier("redis")
    private RedisTemplate redisTemplate;

    @GetMapping("/addcart/{skuId}/{skuNum}")
    public void addCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum, HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if(StringUtils.isEmpty(userId)){
           userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addCart(skuId,skuNum,userId);

    }

    @GetMapping("/addToCart/{skuId}")
    public CartInfo addToCart(@PathVariable Long skuId,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        CartInfo cartInfo = cartService.addToCart(skuId,userId);
        if(cartInfo==null){
            String userTempId = AuthContextHolder.getUserTempId(request);
            cartInfo = cartService.addToCart(skuId,userTempId);
        }
        System.out.println(cartInfo);
        return cartInfo;
    }

    @GetMapping("/cartList")
    public Result mergeCart(HttpServletRequest request){
        String userTempId = AuthContextHolder.getUserTempId(request);
        String token = request.getHeader("token");
        String userId= (String)redisTemplate.opsForValue().get(RedisConst.USER_LOGIN_KEY_PREFIX+token);
        List<CartInfo> cartInfoList = cartService.mergeCart(userTempId,userId);
        return Result.ok(cartInfoList);
    }

    @GetMapping("/checkCart/{skuId}/{isCkecked}")
    public Result checkCart(@PathVariable Long skuId,@PathVariable Integer isCkecked,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        cartService.checkCart(skuId,isCkecked,userId);
       return Result.ok();
    }

    //查询购物车中被用户选中的项
    @GetMapping("/getIsCheckedCart")
    public List<CartInfo> getIsCheckCartList(HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        List<CartInfo> isCheckedCartList = cartService.getIsCheckCartList(userId);
        return isCheckedCartList;
    }


}
